<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>秒杀系统架构设计方案</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 8px;
            padding: 1.5rem;
            overflow-x: auto;
            font-family: 'Consolas', 'Monaco', monospace;
        }
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            bottom: -10px;
            width: 60px;
            height: 4px;
            background: linear-gradient(90deg, #667eea, #764ba2);
            border-radius: 2px;
        }
        .mermaid {
            background: white;
            padding: 2rem;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
        }
        .feature-icon {
            background: linear-gradient(135deg, #667eea, #764ba2);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        .animate-fadeInUp {
            animation: fadeInUp 0.8s ease-out;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6 text-center">
            <h1 class="text-5xl md:text-6xl font-bold mb-6 animate-fadeInUp">
                <i class="fas fa-bolt mr-4"></i>秒杀系统架构设计
            </h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90 animate-fadeInUp" style="animation-delay: 0.2s;">
                应对10000并发请求，100件商品的高性能解决方案
            </p>
            <div class="flex justify-center space-x-8 text-3xl animate-fadeInUp" style="animation-delay: 0.4s;">
                <div class="text-center">
                    <i class="fas fa-users mb-2"></i>
                    <p class="text-sm">10,000 并发</p>
                </div>
                <div class="text-center">
                    <i class="fas fa-shopping-cart mb-2"></i>
                    <p class="text-sm">100 商品</p>
                </div>
                <div class="text-center">
                    <i class="fas fa-tachometer-alt mb-2"></i>
                    <p class="text-sm">毫秒响应</p>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="bg-gray-50 py-16">
        <div class="container mx-auto px-6">
            
            <!-- System Architecture Overview -->
            <section class="mb-16">
                <div class="bg-white rounded-2xl shadow-xl p-8">
                    <h2 class="text-3xl font-bold mb-8 relative section-title">系统架构全景图</h2>
                    <div class="mermaid">
                        graph TB
                            A[用户请求] -->|负载均衡| B[Nginx/HAProxy]
                            B --> C[应用服务器集群]
                            C --> D[Redis缓存层]
                            C --> E[消息队列]
                            D --> F[数据库主从]
                            E --> G[异步处理服务]
                            G --> F
                            
                            style A fill:#f9f,stroke:#333,stroke-width:2px
                            style B fill:#bbf,stroke:#333,stroke-width:2px
                            style D fill:#bfb,stroke:#333,stroke-width:2px
                    </div>
                </div>
            </section>

            <!-- Architecture Design -->
            <section class="mb-16">
                <h2 class="text-4xl font-bold mb-10 text-center">
                    <i class="fas fa-layer-group feature-icon mr-3"></i>架构设计方案
                </h2>
                
                <div class="grid md:grid-cols-2 gap-8">
                    <!-- Load Balancing -->
                    <div class="bg-white rounded-xl shadow-lg p-8 card-hover">
                        <div class="flex items-center mb-6">
                            <i class="fas fa-balance-scale text-3xl text-purple-600 mr-4"></i>
                            <h3 class="text-2xl font-bold">负载均衡策略</h3>
                        </div>
                        <div class="space-y-4">
                            <div class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                                <div>
                                    <h4 class="font-semibold mb-2">Nginx/HAProxy负载均衡器</h4>
                                    <p class="text-gray-600">将用户请求均匀分配到多个应用服务器，避免单点故障和过载问题。</p>
                                </div>
                            </div>
                            <div class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                                <div>
                                    <h4 class="font-semibold mb-2">DNS轮询技术</h4>
                                    <p class="text-gray-600">结合DNS轮询，将请求分发到不同数据中心，提高系统可用性。</p>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Microservices -->
                    <div class="bg-white rounded-xl shadow-lg p-8 card-hover">
                        <div class="flex items-center mb-6">
                            <i class="fas fa-cubes text-3xl text-purple-600 mr-4"></i>
                            <h3 class="text-2xl font-bold">微服务架构</h3>
                        </div>
                        <div class="space-y-4">
                            <div class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                                <div>
                                    <h4 class="font-semibold mb-2">服务分离</h4>
                                    <p class="text-gray-600">秒杀服务、用户服务、库存服务独立部署，各自扩展。</p>
                                </div>
                            </div>
                            <div class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                                <div>
                                    <h4 class="font-semibold mb-2">服务注册与发现</h4>
                                    <p class="text-gray-600">使用Eureka或Consul进行服务管理，提高动态管理能力。</p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </section>

            <!-- High Concurrency Handling -->
            <section class="mb-16">
                <h2 class="text-4xl font-bold mb-10 text-center">
                    <i class="fas fa-rocket feature-icon mr-3"></i>高并发处理方案
                </h2>

                <div class="bg-white rounded-2xl shadow-xl p-10">
                    <div class="grid md:grid-cols-2 gap-10">
                        <!-- Rate Limiting -->
                        <div>
                            <h3 class="text-2xl font-bold mb-6 flex items-center">
                                <i class="fas fa-traffic-light text-yellow-500 mr-3"></i>
                                请求限流与排队
                            </h3>
                            <div class="mb-6">
                                <h4 class="font-semibold mb-3">限流策略</h4>
                                <p class="text-gray-600 mb-4">使用Redis令牌桶算法控制请求速率，例如限制每秒1000个请求。</p>
                                <div class="code-block">
                                    <pre><code>// 使用Redis的List作为队列
String queueKey = "seckill:queue";
jedis.lpush(queueKey, requestData);</code></pre>
                                </div>
                            </div>
                        </div>

                        <!-- Distributed Lock -->
                        <div>
                            <h3 class="text-2xl font-bold mb-6 flex items-center">
                                <i class="fas fa-lock text-red-500 mr-3"></i>
                                分布式锁机制
                            </h3>
                            <div class="mb-6">
                                <h4 class="font-semibold mb-3">Redis分布式锁</h4>
                                <p class="text-gray-600 mb-4">确保库存操作的原子性，避免超卖现象。</p>
                                <div class="code-block">
                                    <pre><code>// 使用Redisson获取分布式锁
RLock lock = redissonClient.getLock("seckill:lock:" + productId);
lock.lock();
try {
    // 执行库存扣减操作
} finally {
    lock.unlock();
}</code></pre>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </section>

            <!-- Inventory Management -->
            <section class="mb-16">
                <h2 class="text-4xl font-bold mb-10 text-center">
                    <i class="fas fa-warehouse feature-icon mr-3"></i>库存管理策略
                </h2>

                <div class="grid md:grid-cols-3 gap-6">
                    <div class="bg-gradient-to-br from-purple-500 to-pink-500 text-white rounded-xl p-6 card-hover">
                        <i class="fas fa-database text-4xl mb-4"></i>
                        <h3 class="text-xl font-bold mb-3">缓存系统</h3>
                        <p>使用Redis缓存商品库存，减少数据库访问频率，提升响应速度。</p>
                    </div>
                    <div class="bg-gradient-to-br from-blue-500 to-cyan-500 text-white rounded-xl p-6 card-hover">
                        <i class="fas fa-sync-alt text-4xl mb-4"></i>
                        <h3 class="text-xl font-bold mb-3">原子操作</h3>
                        <p>使用Redis DECR命令进行原子操作，保证库存一致性。</p>
                    </div>
                    <div class="bg-gradient-to-br from-green-500 to-teal-500 text-white rounded-xl p-6 card-hover">
                        <i class="fas fa-shield-alt text-4xl mb-4"></i>
                        <h3 class="text-xl font-bold mb-3">数据安全</h3>
                        <p>实时监控库存变化，防止数据异常和恶意攻击。</p>
                    </div>
                </div>

                <!-- Code Example -->
                <div class="bg-white rounded-xl shadow-lg p-8 mt-8">
                    <h4 class="text-xl font-bold mb-4">库存管理代码示例</h4>
                    <div class="grid md:grid-cols-2 gap-6">
                        <div class="code-block">
                            <pre><code>// 从Redis缓存中获取库存
String stockKey = "seckill:stock:" + productId;
Integer stock = Integer.parseInt(
    redisTemplate.opsForValue().get(stockKey)
);</code></pre>
                        </div>
                        <div class="code-block">
                            <pre><code>// 扣减库存
Long newStock = redisTemplate
    .opsForValue().decrement(stockKey);
if (newStock < 0) {
    // 库存不足，处理失败情况
}</code></pre>
                        </div>
                    </div>
                </div>
            </section>

            <!-- Security and Performance -->
            <section class="mb-16">
                <h2 class="text-4xl font-bold mb-10 text-center">
                    <i class="fas fa-shield-virus feature-icon mr-3"></i>安全防护与性能优化
                </h2>

                <div class="bg-white rounded-2xl shadow-xl p-10">
                    <div class="mermaid mb-8">
                        graph LR
                            A[用户请求] --> B[验证码校验]
                            B --> C[请求签名验证]
                            C --> D[限流检查]
                            D --> E[业务处理]
                            E --> F[监控记录]
                            
                            style A fill:#f96,stroke:#333,stroke-width:2px
                            style E fill:#9f6,stroke:#333,stroke-width:2px
                    </div>

                    <div class="gri